package stack;

import org.junit.Test;

import java.util.Stack;

/**
 * @className: LeetCode_20
 * @Description: ...
 * @author: kunkun
 * @date: 2025/3/24 23:01
 */
public class LeetCode_20 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i)=='{' || s.charAt(i)=='[' ||s.charAt(i)=='(' ){
                stack.add(s.charAt(i));
            }else {
                if (stack.isEmpty()){
                    return false;
                }
                Character pop = stack.pop();
                if ((s.charAt(i)=='}' && pop == '{')||(s.charAt(i)==']' && pop == '[')||(s.charAt(i)==')' && pop == '(')){
                   continue;
                }else {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }

    @Test
    public void test(){
        System.out.println(isValid("()"));
    }
}
